home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.text;
-
- import com.sun.java.swing.event.DocumentEvent;
- import java.awt.Component;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.Shape;
-
- public class BoxView extends CompositeView {
- int axis;
- int width;
- int height;
- boolean xValid;
- boolean yValid;
- int[] preferredSpan = new int[2];
- int[] resizeWeight = new int[2];
- float[] alignment = new float[2];
- boolean xAllocValid;
- int[] xOffsets;
- int[] xSpans;
- boolean yAllocValid;
- int[] yOffsets;
- int[] ySpans;
-
- public BoxView(Element elem, int axis) {
- super(elem);
- this.axis = axis;
- }
-
- void calculateAlignedPositions(int allocated, int axis) {
- int[] offsets = axis == 0 ? this.xOffsets : this.yOffsets;
- int[] spans = axis == 0 ? this.xSpans : this.ySpans;
- int totalBelow = (int)((float)allocated * this.alignment[axis]);
- int totalAbove = allocated - totalBelow;
- int n = ((CompositeView)this).getViewCount();
-
- for(int i = 0; i < n; ++i) {
- View v = ((CompositeView)this).getView(i);
- float align = v.getAlignment(axis);
- int span = (int)v.getPreferredSpan(axis);
- int below = (int)((float)span * align);
- int above = span - below;
- if (v.getResizeWeight(axis) > 0) {
- below = totalBelow;
- above = totalAbove;
- }
-
- offsets[i] = totalBelow - below;
- spans[i] = below + above;
- }
-
- }
-
- void calculateAlignedSizeRequirements(int axis) {
- int totalAbove = 0;
- int totalBelow = 0;
- int n = ((CompositeView)this).getViewCount();
-
- for(int i = 0; i < n; ++i) {
- View v = ((CompositeView)this).getView(i);
- int span = (int)v.getPreferredSpan(axis);
- int below = (int)(v.getAlignment(axis) * (float)span);
- int above = span - below;
- totalAbove = Math.max(above, totalAbove);
- totalBelow = Math.max(below, totalBelow);
- int[] var10000 = this.resizeWeight;
- var10000[axis] += v.getResizeWeight(axis);
- }
-
- this.preferredSpan[axis] = totalAbove + totalBelow;
- this.alignment[axis] = 0.5F;
- if (this.preferredSpan[axis] > 0) {
- this.alignment[axis] = (float)totalBelow / (float)this.preferredSpan[axis];
- }
-
- }
-
- void calculateTiledPositions(int allocated, int axis) {
- int[] offsets = axis == 0 ? this.xOffsets : this.yOffsets;
- int[] spans = axis == 0 ? this.xSpans : this.ySpans;
- int totalPlay = allocated - this.preferredSpan[axis];
- int totalWeight = this.resizeWeight[axis];
- int totalOffset = 0;
- int n = ((CompositeView)this).getViewCount();
-
- for(int i = 0; i < n; ++i) {
- View v = ((CompositeView)this).getView(i);
- offsets[i] = totalOffset;
- int span = (int)v.getPreferredSpan(axis);
- int weight = v.getResizeWeight(axis);
- if (weight != 0 && totalWeight != 0) {
- float factor = (float)(weight / totalWeight);
- span = (int)((float)span + (float)totalPlay * factor);
- }
-
- spans[i] = span;
- totalOffset += span;
- }
-
- }
-
- void calculateTiledSizeRequirements(int axis) {
- this.alignment[axis] = 0.5F;
- this.preferredSpan[axis] = 0;
- this.resizeWeight[axis] = 0;
- int n = ((CompositeView)this).getViewCount();
-
- for(int i = 0; i < n; ++i) {
- View v = ((CompositeView)this).getView(i);
- int[] var10000 = this.preferredSpan;
- var10000[axis] = (int)((float)var10000[axis] + v.getPreferredSpan(axis));
- var10000 = this.resizeWeight;
- var10000[axis] += v.getResizeWeight(axis);
- }
-
- }
-
- public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
- Element elem = ((View)this).getElement();
- Rectangle alloc = a != null && this.isAllocationValid() ? ((CompositeView)this).getInsideAllocation(a) : null;
- int x = 0;
- int y = 0;
- int width = 0;
- int height = 0;
- if (alloc != null) {
- x = alloc.x;
- y = alloc.y;
- width = alloc.width;
- height = alloc.height;
- }
-
- int index0 = elem.getElementIndex(e.getOffset());
- int index1 = elem.getElementIndex(e.getOffset() + Math.max(e.getLength() - 1, 0));
-
- for(int i = index0; i <= index1; ++i) {
- View v = ((CompositeView)this).getView(i);
- if (alloc != null) {
- alloc.x = x + this.xOffsets[i];
- alloc.y = y + this.yOffsets[i];
- alloc.width = this.xSpans[i];
- alloc.height = this.ySpans[i];
- }
-
- v.changedUpdate(e, alloc, f);
- }
-
- DocumentEvent.ElementChange ec = e.getChange(elem);
- if (ec != null) {
- Element[] removedElems = ec.getChildrenRemoved();
- Element[] addedElems = ec.getChildrenAdded();
- View[] added = new View[addedElems.length];
-
- for(int i = 0; i < addedElems.length; ++i) {
- added[i] = f.create(addedElems[i]);
- }
-
- this.replace(ec.getIndex(), removedElems.length, added);
- }
-
- if (a != null && !this.isAllocationValid()) {
- Component c = ((View)this).getContainer();
- c.repaint(x, y, width, height);
- }
-
- }
-
- void checkRequests() {
- if (this.axis == 0) {
- if (!this.xValid) {
- this.calculateTiledSizeRequirements(0);
- }
-
- if (!this.yValid) {
- this.calculateAlignedSizeRequirements(1);
- }
- } else {
- if (!this.xValid) {
- this.calculateAlignedSizeRequirements(0);
- }
-
- if (!this.yValid) {
- this.calculateTiledSizeRequirements(1);
- }
- }
-
- this.yValid = true;
- this.xValid = true;
- }
-
- protected void childAllocation(int index, Rectangle alloc) {
- alloc.x += this.xOffsets[index];
- alloc.y += this.yOffsets[index];
- alloc.width = this.xSpans[index];
- alloc.height = this.ySpans[index];
- }
-
- public float getAlignment(int axis) {
- this.checkRequests();
- switch (axis) {
- case 0:
- case 1:
- return this.alignment[axis];
- default:
- throw new IllegalArgumentException("Invalid axis: " + axis);
- }
- }
-
- public final int getHeight() {
- return this.height;
- }
-
- public float getPreferredSpan(int axis) {
- this.checkRequests();
- switch (axis) {
- case 0:
- return (float)(this.preferredSpan[axis] + ((CompositeView)this).getLeftInset() + ((CompositeView)this).getRightInset());
- case 1:
- return (float)(this.preferredSpan[axis] + ((CompositeView)this).getTopInset() + ((CompositeView)this).getBottomInset());
- default:
- throw new IllegalArgumentException("Invalid axis: " + axis);
- }
- }
-
- public int getResizeWeight(int axis) {
- this.checkRequests();
- switch (axis) {
- case 0:
- case 1:
- return this.resizeWeight[axis];
- default:
- throw new IllegalArgumentException("Invalid axis: " + axis);
- }
- }
-
- protected View getViewAtPoint(int x, int y, Rectangle alloc) {
- int n = ((CompositeView)this).getViewCount();
- if (this.axis == 0) {
- if (x < alloc.x + this.xOffsets[0]) {
- this.childAllocation(0, alloc);
- return ((CompositeView)this).getView(0);
- } else {
- for(int i = 0; i < n; ++i) {
- if (x < alloc.x + this.xOffsets[i]) {
- this.childAllocation(i - 1, alloc);
- return ((CompositeView)this).getView(i - 1);
- }
- }
-
- this.childAllocation(n - 1, alloc);
- return ((CompositeView)this).getView(n - 1);
- }
- } else if (y < alloc.y + this.yOffsets[0]) {
- this.childAllocation(0, alloc);
- return ((CompositeView)this).getView(0);
- } else {
- for(int i = 0; i < n; ++i) {
- if (y < alloc.y + this.yOffsets[i]) {
- this.childAllocation(i - 1, alloc);
- return ((CompositeView)this).getView(i - 1);
- }
- }
-
- this.childAllocation(n - 1, alloc);
- return ((CompositeView)this).getView(n - 1);
- }
- }
-
- public final int getWidth() {
- return this.width;
- }
-
- public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
- Element elem = ((View)this).getElement();
- DocumentEvent.ElementChange ec = e.getChange(elem);
- if (ec != null) {
- Element[] removedElems = ec.getChildrenRemoved();
- Element[] addedElems = ec.getChildrenAdded();
- View[] added = new View[addedElems.length];
-
- for(int i = 0; i < addedElems.length; ++i) {
- added[i] = f.create(addedElems[i]);
- }
-
- this.replace(ec.getIndex(), removedElems.length, added);
- if (a != null) {
- this.preferenceChanged((View)null, true, true);
- ((View)this).getContainer().repaint();
- }
- }
-
- Rectangle alloc = a != null && this.isAllocationValid() ? ((CompositeView)this).getInsideAllocation(a) : null;
- int pos = e.getOffset();
- View v = ((CompositeView)this).getViewAtPosition(pos, alloc);
- if (v != null) {
- v.insertUpdate(e, alloc, f);
- if (v.getStartOffset() == pos && pos > 0) {
- v = ((CompositeView)this).getViewAtPosition(pos - 1, alloc);
- v.insertUpdate(e, alloc, f);
- }
- }
-
- }
-
- protected boolean isAfter(int x, int y, Rectangle innerAlloc) {
- if (this.axis == 0) {
- return x > innerAlloc.width + innerAlloc.x;
- } else {
- return y > innerAlloc.height + innerAlloc.y;
- }
- }
-
- protected boolean isAllocationValid() {
- return this.xAllocValid && this.yAllocValid;
- }
-
- protected boolean isBefore(int x, int y, Rectangle innerAlloc) {
- if (this.axis == 0) {
- return x < innerAlloc.x;
- } else {
- return y < innerAlloc.y;
- }
- }
-
- protected void layout(int width, int height) {
- this.checkRequests();
- if (this.xSpans == null) {
- int n = ((CompositeView)this).getViewCount();
- this.xSpans = new int[n];
- this.ySpans = new int[n];
- this.xOffsets = new int[n];
- this.yOffsets = new int[n];
- }
-
- if (this.axis == 0) {
- if (!this.xAllocValid) {
- this.calculateTiledPositions(width, 0);
- }
-
- if (!this.yAllocValid) {
- this.calculateAlignedPositions(height, 1);
- }
- } else {
- if (!this.xAllocValid) {
- this.calculateAlignedPositions(width, 0);
- }
-
- if (!this.yAllocValid) {
- this.calculateTiledPositions(height, 1);
- }
- }
-
- this.xAllocValid = true;
- this.yAllocValid = true;
- int n = ((CompositeView)this).getViewCount();
-
- for(int i = 0; i < n; ++i) {
- View v = ((CompositeView)this).getView(i);
- v.setSize((float)this.xSpans[i], (float)this.ySpans[i]);
- }
-
- }
-
- public Shape modelToView(int pos, Shape a) throws BadLocationException {
- if (!this.isAllocationValid()) {
- Rectangle alloc = a.getBounds();
- this.setSize((float)alloc.width, (float)alloc.height);
- }
-
- return super.modelToView(pos, a);
- }
-
- public void paint(Graphics g, Shape allocation) {
- Rectangle alloc = allocation.getBounds();
- this.setSize((float)alloc.width, (float)alloc.height);
- int n = ((CompositeView)this).getViewCount();
- int x = alloc.x + ((CompositeView)this).getLeftInset();
- int y = alloc.y + ((CompositeView)this).getTopInset();
- Rectangle clip = g.getClipBounds();
-
- for(int i = 0; i < n; ++i) {
- alloc.x = x + this.xOffsets[i];
- alloc.y = y + this.yOffsets[i];
- alloc.width = this.xSpans[i];
- alloc.height = this.ySpans[i];
- if (alloc.intersects(clip)) {
- this.paintChild(g, alloc, i);
- }
- }
-
- }
-
- protected void paintChild(Graphics g, Rectangle alloc, int index) {
- View child = ((CompositeView)this).getView(index);
- child.paint(g, alloc);
- }
-
- public void preferenceChanged(View child, boolean width, boolean height) {
- if (width) {
- this.xValid = false;
- this.xAllocValid = false;
- }
-
- if (height) {
- this.yValid = false;
- this.yAllocValid = false;
- }
-
- super.preferenceChanged(child, width, height);
- }
-
- public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
- Element elem = ((View)this).getElement();
- DocumentEvent.ElementChange ec = e.getChange(elem);
- boolean shouldForward = true;
- if (ec != null) {
- Element[] removedElems = ec.getChildrenRemoved();
- Element[] addedElems = ec.getChildrenAdded();
- View[] added = new View[addedElems.length];
-
- for(int i = 0; i < addedElems.length; ++i) {
- added[i] = f.create(addedElems[i]);
- }
-
- this.replace(ec.getIndex(), removedElems.length, added);
- if (added.length != 0) {
- shouldForward = false;
- }
-
- if (a != null) {
- this.preferenceChanged((View)null, true, true);
- ((View)this).getContainer().repaint();
- }
- }
-
- if (shouldForward) {
- Rectangle alloc = a != null && this.isAllocationValid() ? ((CompositeView)this).getInsideAllocation(a) : null;
- int pos = e.getOffset();
- View v = ((CompositeView)this).getViewAtPosition(pos, alloc);
- if (v != null) {
- v.removeUpdate(e, alloc, f);
- }
- }
-
- }
-
- public void replace(int offset, int length, View[] elems) {
- super.replace(offset, length, elems);
- this.xOffsets = null;
- this.xSpans = null;
- this.xValid = false;
- this.xAllocValid = false;
- this.yOffsets = null;
- this.ySpans = null;
- this.yValid = false;
- this.yAllocValid = false;
- }
-
- public void setSize(float width, float height) {
- if ((int)width != this.width) {
- this.xAllocValid = false;
- }
-
- if ((int)height != this.height) {
- this.yAllocValid = false;
- }
-
- if (!this.xAllocValid || !this.yAllocValid) {
- this.width = (int)width;
- this.height = (int)height;
- this.layout(this.width - ((CompositeView)this).getLeftInset() - ((CompositeView)this).getRightInset(), this.height - ((CompositeView)this).getTopInset() - ((CompositeView)this).getBottomInset());
- }
-
- }
-
- public int viewToModel(float x, float y, Shape a) {
- if (!this.isAllocationValid()) {
- Rectangle alloc = a.getBounds();
- this.setSize((float)alloc.width, (float)alloc.height);
- }
-
- return super.viewToModel(x, y, a);
- }
- }
-